package org.jeecg.modules.rooms.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.CommonUtils;
import org.jeecg.modules.rooms.DTO.CesGoodsUnitDto;
import org.jeecg.modules.rooms.DTO.CesGoodsUnitSearchDto;
import org.jeecg.modules.rooms.entity.CesGoodsUnit;
import org.jeecg.modules.rooms.entity.CesStockType;
import org.jeecg.modules.rooms.mapper.CesGoodsUnitMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品单位 服务实现类
 * </p>
 *
 * @author Claude
 * @since 2023-03-08
 */
@Service
public class CesGoodsUnitServiceImpl extends ServiceImpl<CesGoodsUnitMapper, CesGoodsUnit> implements IService<CesGoodsUnit> {
    @Resource
    private CesGoodsUnitMapper cesGoodsUnitMapper;

    /**
     * 查询接口
     * @param searchDto
     * @return
     */
    public Result list(CesGoodsUnitSearchDto  searchDto){
        QueryWrapper<CesGoodsUnit> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isBlank(searchDto.getName())){
            queryWrapper.like(CesGoodsUnit.NAME, CommonUtils.escapeChar(searchDto.getName()));
        }
        queryWrapper.eq(CesGoodsUnit.HOTEL_ID,searchDto.getHotelId());
        queryWrapper.eq(CesGoodsUnit.INVALID,false);
        queryWrapper.orderByDesc(CesGoodsUnit.CREATEAT);

        IPage<CesGoodsUnit> dataPage = cesGoodsUnitMapper.selectPage(new Page<>(searchDto.getPageNo().intValue(),searchDto.getPageSize().intValue()),queryWrapper);
        Map<String,Object> map = new HashedMap();
        map.put("records",dataPage.getRecords());
        map.put("total",dataPage.getTotal());
        return Result.OK(map);
    }

    /**
     * 查询接口
     * @param dto
     * @return
     */
    public Result create(CesGoodsUnitDto dto){
        CesGoodsUnit unit =  cesGoodsUnitMapper.selectOne(Wrappers.<CesGoodsUnit>lambdaQuery()
                        .eq(CesGoodsUnit::getHotelId,dto.getHotelId())
                .eq(CesGoodsUnit::getInvalid,false).eq(CesGoodsUnit::getName,dto.getName()));
        if(!ObjectUtils.isEmpty(unit)) return Result.error("已存在相同名称的单位!");
        CesGoodsUnit cesGoodsUnit = new CesGoodsUnit();
        BeanUtil.copyProperties(dto,cesGoodsUnit);
        cesGoodsUnit.setCreateAt(LocalDateTime.now());
        cesGoodsUnit.setUpdateAt(LocalDateTime.now());
        cesGoodsUnit.setInvalid(false);
        cesGoodsUnitMapper.insert(cesGoodsUnit);
        return Result.OK("保存成功!");
    }


    /**
     * 修改
     * @param dto
     * @return
     */
    public Result modify(CesGoodsUnitDto dto){
        CesGoodsUnit unit = cesGoodsUnitMapper.selectById(dto.getId());
        if(ObjectUtils.isEmpty(unit)) return Result.error("数据未找到!");

        BeanUtil.copyProperties(dto,unit, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        unit.setUpdateAt(LocalDateTime.now());
        cesGoodsUnitMapper.updateById(unit);
        return Result.OK("修改成功!");
    }


    /**
     * 删除单个
     * @param id
     * @return
     */
    public Result delete(String id){
        CesGoodsUnit unit = cesGoodsUnitMapper.selectById(id);
        if(ObjectUtils.isEmpty(unit)) return Result.error("数据未找到!");

        unit.setUpdateAt(LocalDateTime.now());
        unit.setInvalid(true);
        cesGoodsUnitMapper.updateById(unit);
        return Result.OK("删除成功!");
    }


    /**
     * 批量删除
     * @param ids
     * @return
     */
    public Result deleteBatch(List<String> ids){
        List<CesGoodsUnit> units = cesGoodsUnitMapper.selectList(Wrappers.<CesGoodsUnit>lambdaQuery().in(CesGoodsUnit::getId,ids).eq(CesGoodsUnit::getInvalid,false));
        if(CollectionUtil.isEmpty(units)) return Result.error("数据未找到!");

        units.forEach(unit -> {
            unit.setUpdateAt(LocalDateTime.now());
            unit.setInvalid(true);
        });
        saveOrUpdateBatch(units);
        return Result.OK("删除成功!");
    }

    /**
     * 根据IDS 查询单位数据
     * @param ids
     * @return
     */
    public List<CesGoodsUnit> fetchByIds(List<String> ids){
        if(CollectionUtil.isEmpty(ids)) return new ArrayList<>();
        List<CesGoodsUnit> list = cesGoodsUnitMapper.selectList(Wrappers.<CesGoodsUnit>lambdaQuery().eq(CesGoodsUnit::getInvalid,false).in(CesGoodsUnit::getId,ids));

        return  list;
    }

}

